//////////////////////
///     Tables     ///
//////////////////////

clear
global DIR "C:\XXX" 
cd "${DIR}"
//
use "${DIR}\Smaller_dataset.dta" 
eststo clear

//---- Table 1 (identical to line 114 in RESTAT MS24757_PSD_Micro_Regs.do)
eststo, title("Col 1"):  reg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket hp interest PTI age_borrower
eststo, title("Col 2"):  reg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket hp interest PTI age_borrower pc_inc pc_inc_sq 
eststo, title("Col 3"):  reg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket hp interest PTI age_borrower pc_inc pc_inc_sq LTV mean_lti_ym mean_loan_ym
 
estout using "Table1.txt", style(tex) cells(b(fmt(2)) t(par fmt(1))) ///
stats(r2 N) label legend replace

//---- Table B1 - Drop Upper Income Tail

clear
use "${DIR}\Smaller_dataset.dta" 
eststo clear

bysort ym: egen pc_inc_1pp = pctile(pc_inc), p(99)
bysort ym: egen pc_inc_5pp = pctile(pc_inc), p(95)
//
drop if pc_inc > pc_inc_1pp
eststo, title("Inc 1pc"):  reg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket ///
hp interest PTI age_borrower pc_inc pc_inc_sq LTV mean_lti_ym mean_loan_ym
//
drop if pc_inc > pc_inc_5pp
eststo, title("Inc 5pc"):  reg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket ///
hp interest PTI age_borrower pc_inc pc_inc_sq LTV mean_lti_ym mean_loan_ym

estout using "TableB1_Income.txt", style(tex) cells(b(fmt(2)) t(par fmt(1))) ///
stats(r2 N) label legend replace

//---- Table B1 - Drop Lower LTV Tail

clear
use "${DIR}\Smaller_dataset.dta"
eststo clear

bysort ym: egen LTV_1pp = pctile(LTV), p(1)
bysort ym: egen LTV_5pp = pctile(LTV), p(5)

drop if LTV < LTV_1pp
eststo, title("LTV 1pc"):  reg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket ///
hp interest PTI age_borrower pc_inc pc_inc_sq LTV mean_lti_ym mean_loan_ym

drop if LTV < LTV_5pp
eststo, title("LTV 5pc"):  reg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket ///
hp interest PTI age_borrower pc_inc pc_inc_sq LTV mean_lti_ym mean_loan_ym

estout using "TableB1_LTV.txt", style(tex) cells(b(fmt(2)) t(par fmt(1))) ///
stats(r2 N) label legend replace

//---- Table B1 - Quantiles

clear
use "${DIR}\Smaller_dataset.dta"
eststo clear

set seed 123
bysort ym: gen rand = runiform()
sort ym rand
bysort ym: gen n = _n
keep if n <=2000

eststo, title("P25"):  qreg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket hp ///
interest PTI age_borrower pc_inc pc_inc_sq LTV mean_lti_ym mean_loan_ym , quantile(.25)
estout using "TableB1_Qreg_25.txt", style(tex) cells(b(fmt(2)) t(par fmt(1))) ///
stats(r2 N) label legend replace

eststo, title("P50"):  qreg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket hp ///
interest PTI age_borrower pc_inc pc_inc_sq LTV mean_lti_ym mean_loan_ym , quantile(.50)
estout using "TableB1_Qreg_50.txt", style(tex) cells(b(fmt(2)) t(par fmt(1))) ///
stats(r2 N) label legend replace

eststo, title("P75"):  qreg LTI i.yq i.mort i.rate i.reg i.basis i.bank_id i.ltv_bucket hp ///
interest PTI age_borrower pc_inc pc_inc_sq LTV mean_lti_ym mean_loan_ym , quantile(.75)
estout using "TableB1_Qreg_75.txt", style(tex) cells(b(fmt(2)) t(par fmt(1))) ///
stats(r2 N) label legend replace

** ENDS
